﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WebSiteAdminTool.UI.ViewModels.Base;
using System.ComponentModel;
using WebSiteAdminTool.UI.MembershipService;
using System.Collections.ObjectModel;
using WebSiteAdminTool.UI.Utils.Commands;
using WebSiteAdminTool.UI.RolesService;
using WebSiteAdminTool.UI.Services.Messaging;
using WebSiteAdminTool.UI.Services.Messaging.DataManipulationMessages;

namespace WebSiteAdminTool.UI.ViewModels.Security.Users
{
    public class RoleManagementViewModel : BaseViewModel
    {
        #region private data

        // data fields
        private ObservableCollection<RoleData> _rolesList = null;
        private RoleData _selectedRole = null;
        // commands
        private ICommand _doubleClickCommand = null;

        #endregion

        #region public props

        public ICommand DoubleClickCommand { get { return _doubleClickCommand; } }

        public ObservableCollection<RoleData> RolesList
        {
            get { return _rolesList; }
            set
            {
                _rolesList = value;
                RaisePropertyChanged("RolesList");
                RaisePropertyChanged("NumberOfFoundRoles");
            }
        }
        public int NumberOfFoundUsers
        {
            get { return null == RolesList ? 0 : RolesList.Count; }
        }
        public RoleData SelectedRole
        {
            get { return _selectedRole; }
            set
            {
                if (_selectedRole == value)
                {
                    return;
                }

                _selectedRole = value;
                RaisePropertyChanged("SelectedRole");
                MessageManager.SendMessage(new SelectedItemChangedMessage<RoleData>(_selectedRole));
            }
        }

        #endregion

        #region ctors

        public RoleManagementViewModel()
        {
            base.InitViewModel();
        }

        #endregion

        #region initialization

        protected override void InitializeCommands()
        {
            _doubleClickCommand = new ActionLinkClickedCommand(this.EditRole);

            base.InitializeCommands();
        }

        protected override void RegisterListeners()
        {
            MessageManager.RegisterListener<DataObjectCreated<RoleData>>(this, message => LoadData());
            MessageManager.RegisterListener<DataObjectUpdated<RoleData>>(this, message => LoadData());
            MessageManager.RegisterListener<DataObjectDeleted<RoleData>>(this, message => LoadData());

            base.RegisterListeners();
        }

        #endregion

        #region Data Loaders

        protected override void LoadData()
        {
            base.LoadData();

            LoadRoles();
        }

        private void LoadRoles()
        {
            var client = new RolesServiceClient();
            client.GetRolesCompleted +=new EventHandler<GetRolesCompletedEventArgs>(client_GetRolesCompleted);
            client.GetRolesAsync();
        }

        void client_GetRolesCompleted(object sender, GetRolesCompletedEventArgs e)
        {
            if (!e.Cancelled && null == e.Error)
                BindRolesGrid(e.Result);
        }

        #endregion

        #region Data binders

        private void BindRolesGrid(ObservableCollection<RoleData> roles)
        {
            RolesList = roles;
        }

        #endregion

        #region command handlers

        private void EditRole(object data)
        {
            if (null != this.SelectedRole)
            {
                SecurityUiHelpers.ManageRoleDialog(SelectedRole);
            }
        }

        #endregion
    }
}
